<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/11/25 0025
 * Time: 9:19
 */
namespace app\api\controller;

use app\api\model\Api;
use app\api\model\JWT;
use PhpMyAdmin\Server\Status\Data;
use think\App;
use think\Controller;
use think\Db;
use app\api\model\Role as RoleModel;


/**
 * @title 零点
 * @description 接口说明
 * @header name:flag require:1 default: desc:手机设备号
 */
class Zero extends Api
{
    /**
     * @title 零点预约
     * @description 接口说明
     * @author 开发者
     * @url /api/zero/reservation
     * @method POST
     * @param name:eid type:int require:1 default: other: desc:雅间id
     * @param name:end_time type:string require:1 default: other: desc:到店日期
     * @param name:noon type:int require:1 default: other: desc:1中午2下午
     * @param name:people type:string require:1 default: other: desc:预计人数
     * @param name:name type:string require:1 default: other: desc:客户姓名
     * @param name:sex type:string require:1 default: other: desc:性别1男2女
     * @param name:mobile type:string require:1 default: other: desc:手机号
     * @param name:time type:string require:1 default: other: desc:到店时间
     * @param name:director_id type:int require:1 default: other: desc:客户经理id
     * @param name:content type:string require:0 default: other: desc:其他说明
     * @param name:source_id type:int require:0 default: other: desc:客户来源id
     * @param name:type type:int require:0 default: other: desc:1预定2预定并短信
     */
    public function reservation()
    {
            $user = $this->user;
            if( !input('eid') || !input('people') || !input('name') || !input('sex')){
                return $this->ajaxReturn(1,'缺少参数');
            }
            if(!input('noon') ||!input('director_id') || !input('end_time') || !input('time')){
                return $this->ajaxReturn(1,'缺少参数');
            }

            $pid = db::name('admin_role')->where('id','in',$user['role'])->field('pid')->select();
            $pids = 0;
            foreach ($pid as $vs) {
                if($vs['pid'] == 0){
                    $pids = 1;
                    break;
                }else{
                    $pids = 2;
                }
            }
            if($pids == 2){
                $res = RoleModel::roleAuth($user['role']);
                $newArrs = [];
                if($res){
                    foreach ($res as $k => $v) {
                        $str1 =str_replace("[","",$res[$k]);
                        $str1 =str_replace("]","",$str1);
                        $newArr = explode(',',$str1);
                        $newArrs = array_merge($newArr,$newArrs);
                    }
                    foreach ($newArrs as $kn => $vn) {
                        $vn =str_replace('"',"",$vn);
                        $newArrs[$kn] = $vn;
                    }
                    if(!in_array('311',$newArrs)){
                        return $this->ajaxReturn(1,'暂无权限');
                    }
                }
            }
            
            $type = input('type');

            $end_time = input('end_time');
            $end = explode("-",$end_time);
            $update['year'] = $end[0];
            $update['month'] = $end[1];
            $update['day'] = $end[2];
      
            $update['eid'] = input('eid');
            $elegant =  db::name('elegant')->where('id',$update['eid'])->find();

            $update['qid'] = $elegant['qid'];
            $update['pid'] = $elegant['pid'];

            $update['people'] = input('people');
            $update['name'] = input('name');
            $update['sex'] = input('sex');
            $update['mobile'] = input('mobile');
            $update['content'] = input('content');
            $update['noon'] = input('noon');
            $update['addtime'] = time();
            $update['director_id'] = $user['id'];
            $update['director'] = $user['username'];
            $update['time'] = input('time');
            $update['status'] = 1;
            $update['hid'] = $user['hid'];
            $update['source_id'] = input('source_id');
            $uuser = db::name('client_zero')->where('mobile',$update['mobile'])->where('hid',$user['hid'])->find();
            if($type == 2){
                $userd = $update['name'];
                $date = $end_time;
                $hall = $elegant['name'];
                $staff = $update['director'];
                $phone = db::name('user')->where('id',$user['id'])->value('mobiles');
                $mobiles =  sys_check_mobile(input('mobile'));
                if(!$mobiles){
                    return $this->ajaxReturn(1,'手机号格式不正确');
                }
                $mobile = $update['mobile'];
                $msg = ext_send_alismsa($mobile,$userd,$date,$hall,$staff,$phone);
                if($msg['code'] == 'error'){
                    return $this->ajaxReturn(1,$msg['msg']);
                }
            }
            if($uuser){
                $update['uid'] = $uuser['id'];
            }else{
                $list['name'] = input('name');
                $list['mobile'] = input('mobile');
                $list['manager_id'] = $user['id'];
                $list['manager'] = $user['username'];
                $list['hid'] = $user['hid'];
                $list['addtime'] = time();
                $list['source_id'] = input('source_id');
                $uid = db::name('client_zero')->insertGetId($list);
                $update['uid'] = $uid;
            }
           
            db::name('zero')->insert($update);
            return $this->ajaxReturn(0,'成功');
    }
    /**
     * @title 修改订单获取数据
     * @description 接口说明
     * @author 开发者
     * @url /api/zero/modify
     * @method POST
     * @param name:id type:string require:1 default: other: desc:id
     * 
     * @return list:列表@
     * @list id:id elegant:雅间 begin_time:到店时间 people:人数  name:姓名 noon:中午下午 sex:性别1男2女 mobile:手机号 time:到店时间 director_id:预订人id source_id:客户来源  content:其他说明 weekArrs:星期  source:客户来源
     * 
     */
    public function modify()
    {
        $user = $this->user;
        if(!input('id')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        $id = input('id');
        $res = db::name('zero')->where('id',$id)
        ->where('hid',$user['hid'])
        ->field('id,eid,people,name,sex,mobile,content,director_id,director,noon,source_id,time,year,month,day,time')
        ->find();
        if($res){
            if($res['director_id'] != $user['id']){
                $pid = db::name('admin_role')->where('id','in',$user['role'])->field('pid')->select();
                $pids = 0;
                foreach ($pid as $vs) {
                    if($vs['pid'] == 0){
                        $pids = 1;
                        break;
                    }else{
                        $pids = 2;
                    }
                }
                if($pids == 2){
                    $res = RoleModel::roleAuth($user['role']);
                    $newArrs = [];
                    if($res){
                        foreach ($res as $k => $v) {
                            $str1 =str_replace("[","",$res[$k]);
                            $str1 =str_replace("]","",$str1);
                            $newArr = explode(',',$str1);
                            $newArrs = array_merge($newArr,$newArrs);
                        }
                        foreach ($newArrs as $kn => $vn) {
                            $vn =str_replace('"',"",$vn);
                            $newArrs[$kn] = $vn;
                        }
                        if(!in_array('313',$newArrs)){
                            return $this->ajaxReturn(1,'暂无权限');
                        }
                    }
                }
            }
            $res['elegant'] = db::name('elegant')->where('id',$res['eid'])->value('name');
            if($res['noon'] == 1){
                $res['noon'] = '中午';
            }else{
                $res['noon'] = '晚上';
            }
            $res['begin_time'] = $res['year'] . '-' . $res['month'] . '-' . $res['day'];
            $arr=explode("-", $res['begin_time']);//封装成数组
            //参数赋值 
            $year=$arr[0];//年  
            $month=sprintf('%02d',$arr[1]);    //月，输出2位整型，不够2位右对齐  
            $day=sprintf('%02d',$arr[2]);//日，输出2位整型，不够2位右对齐
            $hour = $minute = $second = 0;//时分秒默认赋值为0； 
            $strap = mktime($hour,$minute,$second,$month,$day,$year);//转换成时间戳  
            $number_wk=date("w",$strap);//获取数字型星期几
            //自定义星期数组
            $weekArr=array("周日","周一","周二","周三","周四","周五","周六");
            $weekArrs =  $weekArr[$number_wk];
            $res['weekArr'] = $weekArrs;
            if($res['source_id']){
                $res['source'] = db::name('source')->where('id',$res['source_id'])->value('name');
            }
            unset($res['eid']);
            unset($res['year']);
            unset($res['month']);
            unset($res['day']);
        }
        $data['list'] = $res;
        return $this->ajaxReturn(0,'成功',$data);
    }
       /**
     * @title 修改订单保存
     * @description 接口说明
     * @author 开发者
     * @url /api/zero/modify_save
     * @method POST
     * @param name:id type:string require:1 default: other: desc:id
     * @param name:people type:string require:1 default: other: desc:人数
     * @param name:name type:string require:1 default: other: desc:姓名
     * @param name:sex type:string require:1 default: other: desc:性别
     * @param name:time type:string require:1 default: other: desc:到店时间
     * 
     * 
     * @param name:mobile type:string require:1 default: other: desc:手机号
     * @param name:director_id type:string require:1 default: other: desc:预订人
     * @param name:source_id type:string require:1 default: other: desc:客户来源
     * @param name:content type:string require:1 default: other: desc:其他说明
     * @param name:type type:string require:1 default: other: desc:1预定2预定并短信
     * 
     * 
     */
    public function modify_save()
    {
        $user = $this->user;
        if(!input('id')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        $id = input('id');
        $zero = db::name('zero')->where('id',$id)->find();
        if(!$zero){
            return $this->ajaxReturn(1,'订单异常');
        }
        $type = input('type');
        
        $up['people'] = input('people');
        $up['name'] = input('name');
        $up['time'] = input('time');
        $up['sex'] = input('sex');
        $up['mobile'] = input('mobile');
        $up['director_id'] = input('director_id');
        if($up['director_id']){
            $up['director'] = db::name('user')->where('id',$up['director_id'])->value('username');
        }else{
            $up['director'] = $zero['director'];
        }
        $up['source_id'] = input('source_id');
        $up['content'] = input('content');
        if($type == 2){
            // 预订人
            $userd = input('name');
            $date =  $zero['year'] . '-' . $zero['month'] . '-' . $zero['day'];
            $hall = db::name('elegant')->where('id',$zero['eid'])->value('name');
            $staff = $up['director'];
            $phone = db::name('user')->where('id', $up['director_id'])->value('mobiles');
            $mobile =  input('mobile');
            // $phone = '199';
            $msg = ext_send_alismsa($mobile,$userd,$date,$hall,$staff,$phone);
            if($msg['code'] == 'error'){
                return $this->ajaxReturn(1,$msg['msg']);
            }
        }
        db::name('zero')->where('id',$id)->update($up);
        
        return $this->ajaxReturn(0,'成功');
    }
    /**
     * @title 零点首页
     * @description 接口说明
     * @author 开发者
     * @url /api/zero/index
     * @method POST
     * @param name:year type:string require:1 default: other: desc:年
     * @param name:month type:string require:1 default: other: desc:月
     * @param name:day type:string require:1 default: other: desc:日
     * @param name:qid type:string require:0 default: other: desc:楼层id
     * @param name:pid type:string require:0 default: other: desc:人数id
     * 
     * @return list:列表@
     * @list id:id name:楼层 @return elegant:雅间@
     * @elegant id:id name:名称 pid:人数 @return noon:中午订单@! @return afternoon:下午订单@!
     * @noon id:id director:客户经理  mobile:手机号 sex:性别1男2女  name:预定姓名 status:1预定2入座 content:备注 over:1有翻台0无翻台 
     * @afternoon id:id director:客户经理  mobile:手机号 sex:性别1男2女  name:预定姓名 status:1预定2入座 content:备注 over:1有翻台0无翻台 
     * 
     */
    public function index()
    {
        $user = $this->user;
        if(!input('day') || !input('month') || !input('year')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        $pid = db::name('admin_role')->where('id','in',$user['role'])->field('pid')->select();
        $pids = 0;
        foreach ($pid as $vs) {
            if($vs['pid'] == 0){
                $pids = 1;
                break;
            }else{
                $pids = 2;
            }
        }
        if($pids == 2){
            $res = RoleModel::roleAuth($user['role']);
            $newArrs = [];
            if($res){
                foreach ($res as $k => $v) {
                    $str1 =str_replace("[","",$res[$k]);
                    $str1 =str_replace("]","",$str1);
                    $newArr = explode(',',$str1);
                    $newArrs = array_merge($newArr,$newArrs);
                }
                foreach ($newArrs as $kn => $vn) {
                    $vn =str_replace('"',"",$vn);
                    $newArrs[$kn] = $vn;
                }
                if(!in_array('311',$newArrs)){
                    return $this->ajaxReturn(1,'暂无权限');
                }
            }
        }
        $day = input('day');
        $month = input('month');
        $year = input('year');
        $qid = input('qid');
        $pid = input('pid');
      
      
        
        $where[] = ['hid','=',$user['hid']];
        $where[] = ['is_hidden','=',1];
        $where[] = ['is_del','=',1];
        if($qid){
            $mp[] = ['id','=',$qid];

        }else{
            $mp = [];
        }

        // 楼层
        $area = db::name('area')
        ->where($where)
        ->where($mp)

        ->where('type',1)
        // ->where('id',$qid)

        ->where('is_del',1)
        ->field('id,name')
        ->select();
        // 雅间
        if($qid){
            $where[] = ['qid','=',$qid];
        }
        if($pid){
            $where[] = ['pid','=',$pid];
        }
        $elegant = db::name('elegant')
        ->where($where)
        ->where('is_del',1)
        ->field('id,name,qid,pid')
        ->select();
        // print_R($elegant);die;
        $wheres[] = ['year','=',$year];
        $wheres[] = ['month','=',$month];
        $wheres[] = ['day','=',$day];
        $wheres[] = ['hid','=',$user['hid']];

        $wheres[] = ['status','neq',3];
        if($qid){
            $wheres[] = ['qid','=',$qid];
        }
        if($pid){
            $wheres[] = ['pid','=',$pid];
        }
        // $wheres[] = ['eid','=',4];
        $zero = db::name('zero')
        ->where($wheres)
        ->field('id,eid,director,mobile,sex,name,status,content,director_id,noon')
        ->select();
        $zero =  $this->group_same_key($zero,'eid');
        foreach($zero as $zk=>$zv){
            foreach($elegant as $ek=>$ev){
              
                if($ev['id'] == $zk){
                    $elegant[$ek]['datas'] = $zv;
                }
            }   
        }

         $elegant =  $this->group_same_key($elegant,'qid');
        foreach($area as $k=>$v){
            foreach($elegant as $ke=>$ve){
                if($v['id'] == $ke){
                    $area[$k]['elegant'] = $elegant[$ke];
                }
            }
        }
        $searchtime = $year . '-' . $month . '-' . $day;
        $searchtime = strtotime($searchtime);
        $ares = [];
        $start_time=strtotime(date("Y-m-d",time()));
        $end_time = $start_time+60*60*24;
        foreach($area as $as =>$av){
            if(isset($av['elegant'])){
                foreach ($av['elegant'] as $eak => $eav) {
                   
                        if($eav['pid']){
                             $area[$as]['elegant'][$eak]['pid'] = db::name('area')->where('id',$eav['pid'])->value('name');
                         }
                         $aa = array();
                         $aa = json($aa);
                         if(empty($eav['datas'])){
                             $area[$as]['elegant'][$eak]['afternoon'] = $aa;
                             $area[$as]['elegant'][$eak]['noon'] = $aa;
                         }else{
                             $noon =  $this->group_same_key($eav['datas'],'noon');
                             $count = count($noon);
                      
                             if($count == 1){
                                 foreach($noon as $ok=>$ov){
                                     // 中午
                                     if($ok == 1){
                                         $noonstatus = $this->group_same_key($noon[1],'status');
                                      
                                         if(!empty($noonstatus[2])){
                                             if(!empty($noonstatus[4])){
                                                 $over = 1;
                                             }else{
                                                 $over = 0;
                                             }
                                             if($searchtime >= $start_time && $searchtime <= $end_time){
                                                 
                                                 $area[$as]['elegant'][$eak]['noon'] =  $noonstatus[2][0];
                                                 $area[$as]['elegant'][$eak]['noon']['over'] =  $over;
                                             }else{
                                           
                                                 if($noonstatus[2][0]['status'] == 4){
                                                     $area[$as]['elegant'][$eak]['noon'] =  $noonstatus[2][0];
                                                     $area[$as]['elegant'][$eak]['noon']['over'] =  $over;
                                                 }elseif($noonstatus[2][0]['status'] == 2){
                                                     $area[$as]['elegant'][$eak]['noon'] =  $noonstatus[2][0];
                                                     $area[$as]['elegant'][$eak]['noon']['over'] =  $over;
                                                 }else{
                                                     $area[$as]['elegant'][$eak]['noon'] =  $aa;
                                                 }
                                                
                                             }
                                             
         
                                         }elseif(!empty($noonstatus[4])){
                                             if(!empty($noonstatus[4])){
                                                 $overcount = count($noonstatus[4]);
                                                 if($overcount == 1){
                                                     $over = 0;
     
                                                 }else{
                                                     $over = 1;
                                                 }
                                             }
                                             if($searchtime >= $start_time && $searchtime <= $end_time){
                                                 $area[$as]['elegant'][$eak]['noon'] = $aa;
                                                
                                             }else{
                                                 $area[$as]['elegant'][$eak]['noon'] =  $noonstatus[4][0];
                                                 $area[$as]['elegant'][$eak]['noon']['over'] =  $over;
                                             }
                                         }else{
                                             if(!empty($noonstatus[1])){
                                                 if($searchtime >= $start_time && $searchtime <= $end_time){
     
                                                     $tiema = time();
     
                                                     $times = strtotime(date("Y-m-d",time()). "+16 hour");
                                                     if($tiema > $times){
                                                         $area[$as]['elegant'][$eak]['noon'] = $aa;
                                                     }else{
                                                         $area[$as]['elegant'][$eak]['noon'] =  $noonstatus[1][0];
                                                         $area[$as]['elegant'][$eak]['noon']['over'] =  0;
                                                     }
     
                                                 }else{
                                                     if($noonstatus[1][0]['status'] == 4){
                                                         $area[$as]['elegant'][$eak]['noon'] =  $noonstatus[1][0];
                                                         $area[$as]['elegant'][$eak]['noon']['over'] =  0;
                                                     }else{
                                                         $area[$as]['elegant'][$eak]['noon'] =  $aa;
                                                     }
                                                 }
                                             }else{
                                                 if($searchtime >= $start_time && $searchtime <= $end_time){
                                                     $area[$as]['elegant'][$eak]['noon'] = $aa;
                                                     // $area[$as]['elegant'][$eak]['noon']['over'] =  0;
                                                 }else{
                                                     $area[$as]['elegant'][$eak]['noon'] = $aa;
                                                 }
                                                
         
                                             }
                                         }
                                         if($searchtime >= $start_time && $searchtime <= $end_time){
                                             $area[$as]['elegant'][$eak]['afternoon'] = $aa;
                                         }else{
                                          
                                             $area[$as]['elegant'][$eak]['afternoon'] = $aa;
         
                                         }
                                     // 晚上
                                     }elseif($ok == 2){
                                         $noonstatus = $this->group_same_key($noon[2],'status');
                                         if(!empty($noonstatus[2])){
                                             if(!empty($noonstatus[4])){
                                                 $over = 1;
                                             }else{
                                                 $over = 0;
                                             }
                                             if($searchtime >= $start_time && $searchtime <= $end_time){
                                                 $area[$as]['elegant'][$eak]['afternoon'] =  $noonstatus[2][0];
                                                 $area[$as]['elegant'][$eak]['afternoon']['over'] =  $over;
                                                 // $area[$as]['elegant'][$eak]['noon']['over'] =  $over;
                                             }else{
                                                 if($noonstatus[2][0]['status'] == 4){
                                                     $area[$as]['elegant'][$eak]['afternoon'] =  $noonstatus[2][0];
                                                     $area[$as]['elegant'][$eak]['afternoon']['over'] =  $over;
                                                 }elseif($noonstatus[2][0]['status'] == 2){
                                                     $area[$as]['elegant'][$eak]['afternoon'] =  $noonstatus[2][0];
                                                     $area[$as]['elegant'][$eak]['afternoon']['over'] =  $over;
                                                 }
                                                 else{
                                                     $area[$as]['elegant'][$eak]['afternoon'] =  $aa;
                                                 }
                                             }
                                         }elseif(!empty($noonstatus[4])){
                                             if(!empty($noonstatus[4])){
                                                 $overcount = $noonstatus[4];
                                                 if($overcount == 1){
                                                     $over = 0;
     
                                                 }else{
                                                     $over = 1;
                                                 }
                                             }
                                             if($searchtime >= $start_time && $searchtime <= $end_time){
                                                 if(!empty($noonstatus[1])){
                                                     $tiema = time();
     
                                                     $times = strtotime(date("Y-m-d",time()). "+23 hour");
                                                     if($tiema > $times){
                                                         $area[$as]['elegant'][$eak]['afternoon'] =  $aa;
                                                     }else{
                                                         $area[$as]['elegant'][$eak]['afternoon'] =  $noonstatus[1][0];
                                                         $area[$as]['elegant'][$eak]['afternoon']['over'] =  0;
                                                     }
                                                 }else{
                                                     $area[$as]['elegant'][$eak]['afternoon'] = $aa;
     
                                                 }
                                             }else{
                                                 $area[$as]['elegant'][$eak]['afternoon'] =  $noonstatus[4][0];
                                                 $area[$as]['elegant'][$eak]['afternoon']['over'] =  $over;
                                             }
                                         }else{
                                             if(!empty($noonstatus[1])){
                                                 if($searchtime >= $start_time && $searchtime <= $end_time){
                                                     $tiema = time();
                                                     $times = strtotime(date("Y-m-d",time()). "+23 hour");
                                                     if($tiema > $times){
                                                         $area[$as]['elegant'][$eak]['afternoon'] =  $aa;
                                                     }else{
                                                         $area[$as]['elegant'][$eak]['afternoon'] =  $noonstatus[1][0];
                                                         $area[$as]['elegant'][$eak]['afternoon']['over'] =  0;
                                                     }
                                                 }else{
                                                     if($noonstatus[1][0]['status'] == 4){
                                                         $area[$as]['elegant'][$eak]['afternoon'] =  $noonstatus[1][0];
                                                         $area[$as]['elegant'][$eak]['afternoon']['over'] =  0;
                                                     }elseif($noonstatus[1][0]['status'] == 2){
                                                         $area[$as]['elegant'][$eak]['afternoon'] =  $noonstatus[1][0];
                                                         $area[$as]['elegant'][$eak]['afternoon']['over'] =  0;
                                                     }else{
                                                         $area[$as]['elegant'][$eak]['afternoon'] =  $aa;
                                                     }
                                                 }
             
                                             }else{
                                                 if($searchtime >= $start_time && $searchtime <= $end_time){
                                                     $area[$as]['elegant'][$eak]['afternoon'] = $aa;
                                                 }else{
                                                     $area[$as]['elegant'][$eak]['afternoon'] = $aa;
                                                 }
                                             }
                                         }
                                         if($searchtime >= $start_time && $searchtime <= $end_time){
                                             $area[$as]['elegant'][$eak]['noon'] = $aa;
                                         }else{
                                             $area[$as]['elegant'][$eak]['noon'] = $aa;
                                         }
                                         
                                     }
         
                                 }
                             }elseif($count == 2){
                                 foreach($noon as $ok=>$ov){
                                     // 中午
                                     if($ok == 1){
                                         $noonstatus = $this->group_same_key($noon[1],'status');
                                         // print_R($noonstatus);die;
                                         if(!empty($noonstatus[2])){
                                             if(!empty($noonstatus[4])){
                                                 $over = 1;
                                             }else{
                                                 $over = 0;
                                             }
                                             if($searchtime >= $start_time && $searchtime <= $end_time){
                                                 if($noonstatus[2][0]['status'] == 4){
                                                     $area[$as]['elegant'][$eak]['noon'] =  $noonstatus[2][0];
                                                     $area[$as]['elegant'][$eak]['noon']['over'] =  $over;
                                                 }elseif($noonstatus[2][0]['status'] == 2){
                                                     $area[$as]['elegant'][$eak]['noon'] =  $noonstatus[2][0];
                                                     $area[$as]['elegant'][$eak]['noon']['over'] =  $over;
                                                 }else{
                                                     $area[$as]['elegant'][$eak]['noon'] =  $noonstatus[2][0];
                                                     $area[$as]['elegant'][$eak]['noon']['over'] =  $over;
                                                 }
                                             }else{
                                                 $area[$as]['elegant'][$eak]['noon'] =  $noonstatus[2][0];
                                                 $area[$as]['elegant'][$eak]['noon']['over'] =  $over;
                                             }
         
                                            
                                         }elseif(!empty($noonstatus[4])){
                                             if(!empty($noonstatus[4])){
                                                 $overcount = count($noonstatus[4]);
                                             
                                                 if($overcount == 1){
                                                     $over = 0;
     
                                                 }else{
                                                     $over = 1;
     
                                                 }
                                             }
                                             if($searchtime >= $start_time && $searchtime <= $end_time){
                                                 if(!empty($noonstatus[1])){
                                                     $tiema = time();
                                                     $times = strtotime(date("Y-m-d",time()). "+16 hour");
                                                     if($tiema > $times){
                                                         $area[$as]['elegant'][$eak]['noon'] =  $aa;
                                                     }else{
                                                         $area[$as]['elegant'][$eak]['noon'] =  $noonstatus[1][0];
                                                         $area[$as]['elegant'][$eak]['noon']['over'] =  0;
                                                     }
                                                 }else{
                                                         $area[$as]['elegant'][$eak]['noon'] = $aa;
                                                 }
                                                
                                             }else{
                                                 $noonstatus[4][0]['status'] = 2;
                                                 $area[$as]['elegant'][$eak]['noon'] =  $noonstatus[4][0];
                                                 $area[$as]['elegant'][$eak]['noon']['over'] =  $over;
                                             }
                                         }else{
                                             if(!empty($noonstatus[1])){
                                                 if($searchtime >= $start_time && $searchtime <= $end_time){
                                                     $tiema = time();
                                                     $times = strtotime(date("Y-m-d",time()). "+16 hour");
                                                     if($tiema > $times){
                                                         $area[$as]['elegant'][$eak]['noon'] =  $aa;
                                                     }else{
                                                         $area[$as]['elegant'][$eak]['noon'] =  $noonstatus[1][0];
                                                         $area[$as]['elegant'][$eak]['noon']['over'] =  0;
                                                     }
                                                 }else{
                                                     $area[$as]['elegant'][$eak]['noon'] = $aa;
                                                 }
                                             }else{
                                                 if($searchtime >= $start_time && $searchtime <= $end_time){
                                                     $area[$as]['elegant'][$eak]['noon'] = $aa;
                                                 }else{
                                                     $area[$as]['elegant'][$eak]['noon'] = $aa;
         
                                                 }
                                             }
                                         }
                                     // 晚上
                                     }
                                     if($ok == 2){
                                         $noonstatus = $this->group_same_key($noon[2],'status');
                                         if(!empty($noonstatus[2])){
                                             if(!empty($noonstatus[4])){
                                                 $over = 1;
                                             }else{
                                                 $over = 0;
                                             }
                                             if($searchtime >= $start_time && $searchtime <= $end_time){
                                                 if($noonstatus[2][0]['status'] == 4){
                                                     $area[$as]['elegant'][$eak]['afternoon'] =  $noonstatus[2][0];
                                                     $area[$as]['elegant'][$eak]['afternoon']['over'] =  $over;
                                                 }elseif($noonstatus[2][0]['status'] == 2){
                                                     $area[$as]['elegant'][$eak]['afternoon'] =  $noonstatus[2][0];
                                                     $area[$as]['elegant'][$eak]['afternoon']['over'] =  $over;
                                                 }else{
                                                     $area[$as]['elegant'][$eak]['afternoon'] = $aa;
                                                 }
                                             }else{
                                                 $area[$as]['elegant'][$eak]['afternoon'] =  $noonstatus[2][0];
                                                 $area[$as]['elegant'][$eak]['afternoon']['over'] =  $over;
                                                 
                                             }
                                           
                                         }elseif(!empty($noonstatus[4])){
                                             if(!empty($noonstatus[4])){
                                                 $overcount = $noonstatus[4];
                                                 if($overcount == 1){
                                                     $over = 0;
                                                 }else{
                                                     $over = 1;
                                                 }
                                             }
                                             if($searchtime >= $start_time && $searchtime <= $end_time){
                                                 if(!empty($noonstatus[1])){
                                                     $tiema = time();
                                                     $times = strtotime(date("Y-m-d",time()). "+23 hour");
                                                     if($tiema > $times){
                                                         $area[$as]['elegant'][$eak]['afternoon'] =  $aa;
                                                     }else{
                                                         $area[$as]['elegant'][$eak]['afternoon'] =  $noonstatus[1][0];
                                                         $area[$as]['elegant'][$eak]['afternoon']['over'] =  0;
                                                     }
                                                 }else{
                                                     $area[$as]['elegant'][$eak]['afternoon'] = $aa;
                                                 }
                                                
                                             }else{
                                                 $noonstatus[4][0]['status'] = 2;
                                                 $area[$as]['elegant'][$eak]['afternoon'] =  $noonstatus[4][0];
                                                 $area[$as]['elegant'][$eak]['afternoon']['over'] =  $over;
                                             }
                                         }else{
                                             if(!empty($noonstatus[1])){
                                                 if($searchtime >= $start_time && $searchtime <= $end_time){
                                                     $tiema = time();
                                                     $times = strtotime(date("Y-m-d",time()). "+23 hour");
                                                     if($tiema > $times){
                                                         $area[$as]['elegant'][$eak]['afternoon'] =  $aa;
                                                     }else{
                                                         $area[$as]['elegant'][$eak]['afternoon'] =  $noonstatus[1][0];
                                                         $area[$as]['elegant'][$eak]['afternoon']['over'] =  0;
                                                     }
                                                 }else{
                                                     $area[$as]['elegant'][$eak]['afternoon'] = $aa;
                                                 }
         
                                             }else{
                                                 if($searchtime >= $start_time && $searchtime <= $end_time){
                                                     $area[$as]['elegant'][$eak]['afternoon'] = $aa;
                                                 }else{
                                                     $area[$as]['elegant'][$eak]['afternoon'] = $aa;
         
                                                 }
                                             }
                                         }
                                     }
                                     
                                 }
                             }
                             unset($area[$as]['elegant'][$eak]['datas']);
                         }
                        
                    }

            }else{
                $area[$as]['elegant'] = [];
            }
          
        }
        // print_R($area);die;

        $data['list'] = $area;

        return $this->ajaxReturn(0,'成功',$data);
    }
    public function group_same_key($arr,$key)
    {
        $new_arr = array();
         foreach($arr as $k=>$v ){
            $new_arr[$v[$key]][] = $v;
        }
        return $new_arr;
    }
    /**
     * @title 历史订单
     * @description 接口说明
     * @author 开发者
     * @url /api/zero/history
     * @method POST
     * @param name:year type:string require:1 default: other: desc:年
     * @param name:month type:string require:1 default: other: desc:月
     * @param name:day type:string require:1 default: other: desc:日
     * @param name:qid type:string require:0 default: other: desc:楼层id
     * @param name:eid type:string require:0 default: other: desc:雅间id
     * @param name:noon type:string require:1 default: other: desc:1中午2晚上
     * 
     * 
     * @return list:列表@
     * @list elegant:雅间名称 name:客户姓名 mobile:手机号 people:预计人数 end:到店时间  content:备注 director:预订人客户经理 addtime:预定时间
     */
    public function history()
    {
        $user = $this->user;
        if(!input('day') || !input('month') || !input('year') || !input('noon')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        $day = input('day');
        $month = input('month');
        $year = input('year');
        $qid = input('qid');
        $eid = input('eid');
        $noon = input('noon');
        if($qid){
            $where[] = ['qid','=',$qid];
        }
        if($eid){
            $where[] = ['eid','=',$eid];
        }
        $where[] = ['year','=',$year];
        $where[] = ['month','=',$month];
        $where[] = ['day','=',$day];
        $where[] = ['hid','=',$user['hid']];
        $where[] = ['noon','=',$noon];
        // $where[] = ['status','=',4];
        $where[] = ['hid','=',$user['hid']];
        $data = db::name('zero')->where($where)
        ->where('status','in','2,4')->
        field('eid,name,sex,mobile,people,time,content,director,addtime,month,day')->select();
        if($data){
            foreach ($data as $k => $v) {
                $data[$k]['elegant'] = db::name('elegant')->where('id',$v['eid'])->value('name');
                $data[$k]['addtime'] = date("Y-m-d H:i:s",$v['addtime']);
                $data[$k]['end'] = $v['month'] . '月' . $v['day'] . '日' . ' ' . $v['time'];
                unset($data[$k]['month']);
                unset($data[$k]['day']);
                unset($data[$k]['eid']);
                unset($data[$k]['time']);
            }
        }
        $list['list'] = $data;
        return $this->ajaxReturn(0,'成功',$list);
    }
      /**
     * @title 零点客户来源
     * @description 接口说明
     * @author 开发者
     * @url /api/zero/source
     * @method POST
     * 
     * @return list:人数@
     * @list id:id name:名称
     */
    public function source()
    {
        $user = $this->user;
        $where[] = ['hid','=',$user['hid']];
        $where[] = ['is_hidden','=',1];
        $where[] = ['type','=',2];
        $where[] = ['is_del','=',1];

        $source = db::name('source')->where($where)->field('id,name')->select();
        $data['list'] = $source;
        return $this->ajaxReturn(0,'成功',$data);

    }
    /**
     * @title 零点首页获取雅间与人数
     * @description 接口说明
     * @author 开发者
     * @url /api/zero/elegant
     * @method POST
     * 
     * @return list:人数@
     * @list id:人数id name:名称
     * @return area:楼层@
     * @area id:楼层id name:名称
     */
    public function elegant()
    {
        $user = $this->user;
        $where[] = ['hid','=',$user['hid']];
        $where[] = ['is_hidden','=',1];
        $where[] = ['type','=',1];
        $where[] = ['is_del','=',1];

        $area = db::name('area')->where($where)->field('id,name')->select();
        $wheres[] = ['hid','=',$user['hid']];
        $wheres[] = ['is_hidden','=',1];
        $wheres[] = ['type','=',2];
        $wheres[] = ['is_del','=',1];

        $areas = db::name('area')->where($wheres)->field('id,name')->select();
        $data['area'] = $area;
        $data['list'] = $areas;
        return $this->ajaxReturn(0,'成功',$data);

    }
    /**
     * @title 零点预定信息
     * @description 接口说明
     * @author 开发者
     * @url /api/zero/order_msg
     * @method POST
     * 
     * @param name:id type:string require:1 default: other: desc:订单id首页中oid
     * 
     * @return list:人数@
     * @list id:订单id elegant:雅间名称 name:客户姓名 noon:1中午2晚上 area:楼层 mobile:客户手机号 people:人数 end_time:到店时间 begin:2021-05-13 time:到店时间 content:备注 yu_name:预定人 addtime:预定时间 director:客户经理 director_id:客户经理id
     */
    public function order_msg()
    {
        $user = $this->user;
        if(!input('id')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        $id = input('id');
        $dir = db::name('zero')->where('id',$id)->field('director_id,status')->find();
        if(!$dir){
            return $this->ajaxReturn(1,'订单异常');
        }
        $where[] = ['hid','=',$user['hid']];
        $where[] = ['id','=',$id];
        $list = db::name('zero')->where('id',$id)->field('id,qid,eid,name,mobile,people,time,year,month,day,content,yu_name,addtime,sex,director,director_id,noon')->find();
        if($dir['director_id'] != $user['id']){
            $pid = db::name('admin_role')->where('id','in',$user['role'])->field('pid')->select();
            $pids = 0;
            foreach ($pid as $vs) {
                if($vs['pid'] == 0){
                    $pids = 1;
                    break;
                }else{
                    $pids = 2;
                }
            }
            if($pids == 2){
                $res = RoleModel::roleAuth($user['role']);
                $newArrs = [];
                if($res){
                    foreach ($res as $k => $v) {
                        $str1 =str_replace("[","",$res[$k]);
                        $str1 =str_replace("]","",$str1);
                        $newArr = explode(',',$str1);
                        $newArrs = array_merge($newArr,$newArrs);
                    }
                    foreach ($newArrs as $kn => $vn) {
                        $vn =str_replace('"',"",$vn);
                        $newArrs[$kn] = $vn;
                    }
                    if(!in_array('320',$newArrs)){
                        $list['name'] = '***';
                    }
                    if(!in_array('322',$newArrs)){
                        $list['content'] = '***';
                    }
                  
                    if(!in_array('323',$newArrs)){
                        $list['mobile'] =  preg_replace('/(\d{3})\d{4}(\d{4})/', '$1****$2', $list['mobile']);
                    }
                }
            } 
        }

        $list['area'] = db::name('area')->where('id',$list['qid'])->value('name');
        $list['elegant'] = db::name('elegant')->where('id',$list['eid'])->value('name');
        $list['end_time'] = $list['month'] . '月' . $list['day'] . '日';
        $list['addtime'] = date('Y-m-d H:i:s',$list['addtime']);
        $list['begin'] =   $list['year']. '-' . $list['month'] . '-' . $list['day'];
        unset($list['eid']);
        unset($list['month']);
        unset($list['day']);
        $data['list'] = $list;
        return $this->ajaxReturn(0,'成功',$data);
    }
    /**
     * @title 零点入座与退订
     * @description 接口说明
     * @author 开发者
     * @url /api/zero/seat
     * @method POST
     * 
     * @param name:id type:string require:1 default: other: desc:预定信息中的id
     * @param name:type type:string require:1 default: other: desc:1入座2退订
     * 
     */
    public function seat()
    {
        $user = $this->user;
        if(!input('id')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        if(!input('type')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        $id = input('id');
        $type = input('type');
        $res = db::name('zero')->where('id',$id)->field('director_id,status,year,month,day,noon')->find();

        if($res['director_id'] != $user['id']){
            $pid = db::name('admin_role')->where('id','in',$user['role'])->field('pid')->select();
            $pids = 0;
            foreach ($pid as $vs) {
                if($vs['pid'] == 0){
                    $pids = 1;
                    break;
                }else{
                    $pids = 2;
                }
            }
            if($pids == 2){
                $res = RoleModel::roleAuth($user['role']);
                $newArrs = [];
                if($res){
                    foreach ($res as $k => $v) {
                        $str1 =str_replace("[","",$res[$k]);
                        $str1 =str_replace("]","",$str1);
                        $newArr = explode(',',$str1);
                        $newArrs = array_merge($newArr,$newArrs);
                    }
                    foreach ($newArrs as $kn => $vn) {
                        $vn =str_replace('"',"",$vn);
                        $newArrs[$kn] = $vn;
                    }
                    // 入座全部
                    if($type == 1){
                        if(!in_array('315',$newArrs)){
                            return $this->ajaxReturn(1,'暂无权限');
                            
                        }
                    //退订全部
                    }elseif($type == 2){
                        if(!in_array('319',$newArrs)){
                            return $this->ajaxReturn(1,'暂无权限');
                        }
                    }
                }
            } 
        }
        if($type == 1){
            // 2021 05 11 15
            $time = time();
    
            // 中午
            if($res['noon'] == 1){
                $end_time = strtotime(date("Y-m-d",time()). "+16 hour");
                // 2021 05 11 16
                if($time < $end_time){
                    $up['status'] = 2;
                }else{
                    return $this->ajaxReturn(1,'已不可入座');
                }
            // 下午
            }else{
                $end_times = strtotime(date("Y-m-d",time()). "+16 hour");
                $end_time = strtotime(date("Y-m-d",time()). "+23 hour");
                if($time >= $end_times && $time<= $end_time){
                    $up['status'] = 2;
                }else{
                    return $this->ajaxReturn(1,'已不可入座');
                }
            }

        }elseif($type == 2){
            $up['status'] = 3;
        }

        db::name('zero')->where('id',$id)->update($up);
        return $this->ajaxReturn(0,'成功');
    }
    /**
     * @title 翻台
     * @description 接口说明
     * @author 开发者
     * @url /api/zero/over
     * @method POST
     * 
     */
    public function over()
    {
        $user = $this->user;
        if(!input('id')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        $id = input('id');
        $res = db::name('zero')->where('id',$id)->field('director_id,status,noon')->find();
        if($res['director_id'] != $user['id']){
            $pid = db::name('admin_role')->where('id','in',$user['role'])->field('pid')->select();
            $pids = 0;
            foreach ($pid as $vs) {
                if($vs['pid'] == 0){
                    $pids = 1;
                    break;
                }else{
                    $pids = 2;
                }
            }
            if($pids == 2){
                $res = RoleModel::roleAuth($user['role']);
                $newArrs = [];
                if($res){
                    foreach ($res as $k => $v) {
                        $str1 =str_replace("[","",$res[$k]);
                        $str1 =str_replace("]","",$str1);
                        $newArr = explode(',',$str1);
                        $newArrs = array_merge($newArr,$newArrs);
                    }
                    foreach ($newArrs as $kn => $vn) {
                        $vn =str_replace('"',"",$vn);
                        $newArrs[$kn] = $vn;
                    }
                    if(!in_array('317',$newArrs)){
                        return $this->ajaxReturn(1,'暂无权限');
                        
                    }
                }
            } 
        }

        $time = time();
        if($res['noon'] == 1){
            // 2021 05 11 16
            $end_time =  strtotime(date("Y-m-d",time()). "+16 hour");;
            if($time < $end_time){
                $up['status'] = 4;
            }else{
                return $this->ajaxReturn(1,'已不可翻台');
            }
        // 下午
        }else{
            $end_times =  strtotime(date("Y-m-d",time()). "+16 hour");
            $end_time =   strtotime(date("Y-m-d",time()). "+23 hour");
            if($time >= $end_times && $time<= $end_time){
                $up['status'] = 4;
            }else{
                return $this->ajaxReturn(1,'已不可翻台');
            }
        }


        $up['fan_time'] = time();
        db::name('zero')->where('id',$id)->update($up);
        return $this->ajaxReturn(0,'成功');
    }
    /**
     * @title 查看客户经理
     * @description 接口说明
     * @author 开发者
     * @url /api/zero/view
     * @method POST
     * 
     */
    public function view()
    {
        $user = $this->user;
        $manager = db::name('user')->where('hid',$user['hid'])->field('id,username')->select();
        $data['list'] = $manager;
        return $this->ajaxReturn(0,'成功',$data);
    }

}